package com.asa.ui.F;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.asa.ui.frame.AlgoVisHelper;

public class AlgoFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3035088527551930125L;
	private int canvasWidth;//画布的宽
	private int canvasHeight;//画布的长
	private MineSweeperData data;//雷区的数据类型
   public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void render(MineSweeperData data){
		this.data=data;
		repaint();
	}
   public AlgoFrame(String title,int  canvasWidth, int canvasHeight){
	  super(title);
	  this.canvasWidth=canvasWidth;
	  this.canvasHeight=canvasHeight;
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setResizable(false);//关闭可拖动
	  setVisible(true);
	  AlgoCanvas algoCanvas = new AlgoCanvas();
	  setContentPane(algoCanvas);
	  pack();
   }
   //设置面板JPanel
   class AlgoCanvas extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6056196800598676936L;
    private  AlgoCanvas(){
    	//开启双缓存
    	super(true);
    }
	@Override
	//在面板上绘制内容
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D) g;
		//每个单元占的宽
		int w=canvasWidth/data.getN();
		//每个单元占的高
		int h=canvasHeight/data.getM();
		//绘制逻辑
		for(int i=0;i<data.getM();i++){
			for(int j=0;j<data.getN();j++){
				//如果打开
				if(data.open[i][j]){
				if(data.isMine(i, j)){
					AlgoVisHelper.drawImage(g2d,ImageUrlUtil.MINE_IMAGE,j*w,i*h,w,h);
				}else{
					AlgoVisHelper.drawImage(g2d,ImageUrlUtil.getMineNumber(data.number[i][j]),j*w,i*h,w,h);
				}
			}else{
				if(data.flag[i][j]){
					AlgoVisHelper.drawImage(g2d,ImageUrlUtil.FLAG_IMAGE,j*w,i*h,w,h);
				}else{
					AlgoVisHelper.drawImage(g2d,ImageUrlUtil.BLOCK_IMAGE,j*w,i*h,w,h);
				}
			}
			}
		}
	}
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(canvasWidth,canvasHeight);
	}
   }
   //用来弹出各种信息
   public void showInfo(String info){
	   JOptionPane.showMessageDialog(this, info);
   }
}

